﻿using System.Windows.Documents;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Com.CodePlex.SPC3.SPbAM.View {

	public partial class ServerWorkspaceView : System.Windows.Controls.UserControl {

		public ServerWorkspaceView() {
			InitializeComponent();

			//http://stackoverflow.com/questions/642498/how-to-keep-wpf-textbox-selection-when-not-focused
            logbox.LostFocus += (s, e) => {
				e.Handled = true;

				/*
				Binding binding = BindingOperations.GetBinding(this, TextProperty);
				if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default || binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus) { 
					BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource(); 
				} 
				 * */
			};
            logbox.TextChanged += (s, e) => {
                logbox.ScrollToEnd();
            };
            logbox.DataContextChanged += (s, e) => {
                logbox.ScrollToEnd();
            };
        }

		private void TreeViewItem_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
			TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
			if (treeViewItem != null) {
				treeViewItem.Focus();
				e.Handled = true;
			}
		}

		private static DependencyObject VisualUpwardSearch<T>(DependencyObject source) {
			while (source != null && source.GetType() != typeof(T)) {
				source = VisualTreeHelper.GetParent(source);
			}
			return source;
		}
	}
}